package com.glsc.ngateway.opmanage.service.ledger;


import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuth;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthStaff;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthTemplate;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.ledger.LedgerAuthRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class LedgerAuthService {

    @Resource
    private LedgerAuthRepository ledgerAuthRepository;

    @Resource
    private LedgerAuthTemplateService ledgerAuthTemplateService;

    @Resource
    private LedgerAuthStaffService ledgerAuthStaffService;


    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<LedgerAuth> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<LedgerAuth> page = ledgerAuthRepository.findAll(SpecificationUtil.buildSpecification(param, LedgerAuth.class), pageable);

        return page;
    }

    /**
     * 根据id查所有的权限信息
     * @param ids
     * @return
     */
    public List<LedgerAuth> listByIds(List<Integer> ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("IN_id", StringUtils.join(ids, ","));

        List<LedgerAuth> auths = ledgerAuthRepository.findAll(SpecificationUtil.buildSpecification(param, LedgerAuth.class));

        return auths;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public LedgerAuth findById(Integer id) {
        Optional<LedgerAuth> optional = ledgerAuthRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 根据用户id查询所有相关的交易台站模板信息
     *
     * @param userOaAccount
     * @return
     */
    public List<LedgerAuthTemplate> findByUserId(String userOaAccount) {
        List<LedgerAuthStaff> staffs = ledgerAuthStaffService.listByUserOaAccount(userOaAccount);
        if (CollectionUtils.isEmpty(staffs)) {
            return null;
        }

        List<LedgerAuthTemplate> templates = ledgerAuthTemplateService.listByAuthIds(staffs.stream().map(LedgerAuthStaff::getLedgerAuthId).collect(Collectors.toList()));
        return templates;
    }


    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public LedgerAuth save(LedgerAuth obj) {

        LedgerAuth res = ledgerAuthRepository.save(obj);
        return res;
    }


    /**
     * 根据id删除
     *
     * @param id
     */
    @Transactional
    public void deleteById(Integer id) {
        ledgerAuthRepository.deleteById(id);

        List<LedgerAuthStaff> ledgerAuthStaffs = ledgerAuthStaffService.listByAuthId(id);
        if (CollectionUtils.isNotEmpty(ledgerAuthStaffs)) {
            ledgerAuthStaffService.deleteByIds(ledgerAuthStaffs.stream().map(LedgerAuthStaff::getId).collect(Collectors.toList()));
        }

        List<LedgerAuthTemplate> ledgerAuthTemplates = ledgerAuthTemplateService.listByAuthId(id);
        if (CollectionUtils.isNotEmpty(ledgerAuthTemplates)) {
            ledgerAuthTemplateService.deleteByIds(ledgerAuthTemplates.stream().map(LedgerAuthTemplate::getId).collect(Collectors.toList()));
        }
    }


}
